package com.admin.controller;

import com.admin.commen.BaseController;
import com.admin.service.ContentNewsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fegin.VO.admin.ContentNewsVO;
import com.fegin.bean.admin.ContentNews;
import com.util.Respons.ResponseMsg;
import com.util.Respons.ResponseUtil;
import com.util.pageinfoutil.PageUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 后台新闻管理
 *
 * @author 卫星
 * @package com.admin.controller
 * @date 2019-03-04  14:11
 * @project CurrenCy-Cloud
 */
@Controller
@RequestMapping(value = "admin/news")
public class AdminContentNewsController extends BaseController {

    @Autowired
    private ContentNewsService contentNewsService;

    /**
     * 新闻查看
     *
     * @param contentNews
     * @return
     */
    @RequestMapping(value = "view", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> view(@RequestBody ContentNews contentNews) {
        if (contentNews.getId() == null) {
            return ResponseUtil.getNotNormalMap(ResponseMsg.EMPTY_PARAM);
        }
        ContentNews news = contentNewsService.getById(contentNews.getId());
        if (news != null) {
            return ResponseUtil.getSuccessMap(news);
        }
        return ResponseUtil.getNotNormalMap();
    }

    /**
     * 分页查看数据
     */
    @RequestMapping(value = "newsList", method = {RequestMethod.POST})
    @ResponseBody
    @ApiOperation(notes = "文章查询 ：开始时间 startDate ；结束时间 endDate ;页数 page 每页数量 pageSize", value = "文章查询")
    public Map<String, Object> newsList(@RequestBody ContentNewsVO contentNewsVO) {
        Page page = PageUtil.getMybatisPusPage(contentNewsVO.getPageUtil());
        // 时间过滤参数
        Page<ContentNews> list = contentNewsService.findByCondition(contentNewsVO.getContentNews(), page);
        return ResponseUtil.getSuccessMap(list);
    }

    /**
     * 新闻删除
     *
     * @param contentNews
     * @return
     */
    @RequestMapping(value = "del", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(notes = "删除文章 ：文章Id id；", value = "删除文章")
    public Map<String, Object> delNews(@RequestBody ContentNews contentNews) {
        int data = contentNewsService.deleteByPrimaryKey(contentNews);
        if (data > 0) {
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getNotNormalMap();
    }


}
